﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnisysMassiveImporter_v2.Entidades.Mensajes;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;
using UnisysMassiveImporter_v2.Entidades.Configuracion;

namespace UnisysMassiveImporter_v2.Datos.Archivos
{
    public static class Logger
    {

        //decodifica los tipos de error
        public static String ErrorMessage(int ErrorType)
        {
            switch (ErrorType)
            {
                case 1:
                    return "System Error";
                case 2:
                    return "Configuration Error";
                case 3:
                    return "IO Error";
                case 4:
                    return "DB Error";
                case 5:
                    return "IIF Error";
                case 6:
                    return "Success Message";
                case 7:
                    return "Information Message";
                default:
                    return "Error de configuracion: Error Type solo puede ser de 1 - 8. Valor Acutal: " + ErrorType;
            }
        }

        //determina si escribir dependiendo del level log
        public static bool ifWrite(int errorType)
        {
            String registry = "HKEY_LOCAL_MACHINE\\Software\\Unisys\\Unisys Massive Importer";
            try
            {
                switch (errorType)
                {
                    case 1:
                        {
                            if (Boolean.Parse(Registry.GetValue(registry, "System_Error", false).ToString()))
                                return true;
                            else
                                return false;
                        }
                    case 2:
                        {
                            if (Boolean.Parse(Registry.GetValue(registry, "Configuration_Error", false).ToString()))
                                return true;
                            else
                                return false;
                        }
                    case 3:
                        {
                            if (Boolean.Parse(Registry.GetValue(registry, "IO_Error", false).ToString()))
                                return true;
                            else
                                return false;
                        }
                    case 4:
                        {
                            if (Boolean.Parse(Registry.GetValue(registry, "DB_Error", false).ToString()))
                                return true;
                            else
                                return false;
                        }
                    case 5:
                        {
                            if (Boolean.Parse(Registry.GetValue(registry, "IIF_Error", false).ToString()))
                                return true;
                            else
                                return false;
                        }
                    case 6:
                        {
                            if (Boolean.Parse(Registry.GetValue(registry, "Success_Message", false).ToString()))
                                return true;
                            else
                                return false;
                        }
                    case 7:
                        {
                            if (Boolean.Parse(Registry.GetValue(registry, "Information_Message", false).ToString()))
                                return true;
                            else
                                return false;
                        }
                    default:
                        {
                            throw new Exception("Error del sistema: el tipo de error solo puede ser de 1 - 7. Valor Acutal: " + errorType);
                        }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error al intentar leer el registro de windows: " + e.Message);
            }
        }



        public static bool ifFileWrite()
        {
            String registry = "HKEY_LOCAL_MACHINE\\Software\\Unisys\\Unisys Massive Importer";
            try
            {
                return Boolean.Parse(Registry.GetValue(registry, "FileSystemLog", false).ToString());
            }
            catch (Exception e)
            {
                return false;
            }
        }


        //escribe en file system
        public static void fileWrite(AMessage message,ref Configuration appConfig )
        {
            int _logCounter = 0;
            string logFilename = appConfig.logLocation + "\\" + appConfig.system + "-" + message.threadName + ".Log";
            try
            {
                if (!(Directory.Exists(appConfig.logLocation)))
                    Directory.CreateDirectory(appConfig.logLocation);

                using (StreamWriter swLocalLog = new StreamWriter(logFilename, true))
                {
                    swLocalLog.WriteLine(message.toString());
                    swLocalLog.Flush();
                }

                FileInfo fileProps = new FileInfo(logFilename);
                if (fileProps.Length > (appConfig.logSize * 1000))
                {
                    while (true)
                        try
                        {
                            File.Move(logFilename, logFilename + _logCounter.ToString() + ".old");
                            break;
                        }
                        catch { _logCounter++; }
                }
            }

            catch (Exception exc)
            {
                throw new Exception("Imposible escribir en el logFile " + exc.Message);
            }

        }

      


        /// <summary>
        /// Write events to Windows Event Viewer, reading configurations file for the application.
        /// </summary>
        /// <param name="msgText">Text to be written to the log.</param>
        public static void LogThis4Windows(string msgText, string _eventSourceName, string _eventLogName)
        {
            try
            {
                EventLog _myServiceWindowsLog = new EventLog();
                if (!EventLog.SourceExists(_eventSourceName))
                {
                    EventLog.CreateEventSource(_eventSourceName, _eventLogName);
                }
                _myServiceWindowsLog.Log = _eventLogName;
                _myServiceWindowsLog.Source = _eventSourceName;
                _myServiceWindowsLog.WriteEntry(msgText, EventLogEntryType.Error);
            }
            catch (Exception exc)
            {
            }
        }
    }
}